package com.yiyibook.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yiyibook.controller.vo.OutOrderVO;
import com.yiyibook.entity.Book;
import com.yiyibook.entity.OrderHead;
import com.yiyibook.mapper.OrderHeadMapper;
import com.yiyibook.service.OrderHeadService;
import com.yiyibook.util.ResultEntity;
import com.yiyibook.util.SetPageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 〈功能简述〉
 *
 * @author xun.gong@hand-china.com
 * @date 2019/10/15
 */
@Service
public class OrderHeadServiceImpl implements OrderHeadService {

    @Autowired
    private OrderHeadMapper orderHeadMapper;

    @Override
    public OrderHead selectLast() {
        return orderHeadMapper.selectLast();
    }

    @Override
    public OrderHead insert(OrderHead orderHead) {
        orderHeadMapper.insertSelective(orderHead);
        return orderHead;
    }

    @Override
    public List<OrderHead> selectAll() {
        return orderHeadMapper.selectAll();
    }

    @Override
    public List<OutOrderVO> selectByHeadId(Integer orderHeadId) {
        return orderHeadMapper.selectByHeadId(orderHeadId);
    }

    @Override
    public OrderHead selectById(Integer orderHeadId) {
        return orderHeadMapper.selectByPrimaryKey(orderHeadId);
    }

    @Override
    public int updateByCondition(OrderHead orderHead) {
        return orderHeadMapper.updateByPrimaryKeySelective(orderHead);
    }

    @Override
    public ResultEntity<OrderHead> selectOrderHeadByCondition(PageInfo pageInfo, OrderHead orderCondition) {
        // 保存查询结果以及分页信息
        ResultEntity<OrderHead> resultEntity = new ResultEntity<>();
        // 当前页
        int pageNum = 1;
        // 页面大小
        int pageSize = 5;
        // 判断是否有分页信息
        Integer[] pageMsg = SetPageInfo.setValue(pageInfo, pageNum, pageSize);
        // 分页插件
        PageHelper.startPage(pageMsg[0],pageMsg[1]);
        // 查询所有的订单头信息 取 第 pageNum 页 ，页面大小为 pageSize
        List<OrderHead> orderHeads = orderHeadMapper.selectByCondition(orderCondition);
        // 获取新的分页信息
        PageInfo<OrderHead> pageInfoNew = new PageInfo<>(orderHeads);
        resultEntity.setEntityList(orderHeads);
        resultEntity.setPageInfo(pageInfoNew);
        resultEntity.setQueryCondition(orderCondition);
        return resultEntity;

    }
}
